from load import*

class Game:
    def __init__(self, players, numarms = 50):
        self.players = players
        self.cardCounter = 5
        self.turn = 0 
        self.board,p = randomPlace(len(self.players))
        totalarms = numarms * len(self.players) - len(allTerritories)
        for t in range(totalarms):
           a = self.players[p].deployTo(self, 1)
           tet = a[0]
           playernum, arms = self.board[tet]
           self.board[tet] = (playernum, arms +1)
           p = p + 1 % (len(self.players) -1)
        for t in self.board:
            (plyr,a) = self.board[t]
            plyrObj = self.players[plyr]
            plyrObj.totalTerrs = plyrObj.totalTerrs +1
            plyrObj.totalArmies = plyrObj.totalArmies +a

    def getBoard(self):
            return self.board

    def setBoard(self, board):
            self.board = board

def allotedArmies(game, plyr):
    na = plyrTersN(game, plyr) 
    drafted = int(na/3)
    if drafted < 3:
        drafted = 3
    for c in allContinents:
        if isInControlOfCont(game, plyr, c):
            drafted = drafted + extraArmiesCont[c]
    if cardCashin(plyr):
        drafted = drafted + cardCounter
        game.cardCounter = game.cardCounter + 5
    return drafted

def randomPlace(numplayers):     
    notOwnedT = allTerritories[:]
    p = 0
    board = {}

    while(len(notOwnedT) != 0):
        tet = random.choice(notOwnedT)
        #print tet
        notOwnedT.remove(tet)
        #print str(notOwnedT)
        p = p + 1
        player = p % numplayers
        board[tet] = (player, 1)
    return board, player

def isInControlOfCont(game, p, c): #This code checks and tells who is in controll of a continent
    board = game.getBoard()
    for t in territories[c]:
        (tP, tA) = board[t] #something with territory armies and something named tP
        if tP != p.n:
           return False
    return True

def plyrTers(game, p):
    ts = []
    board = game.getBoard()
    for t in allTerritories:
        (tP, tA) = board[t]
        if tP == p.n:
            ts.append(t)
    return ts

def plyrTersN(game, p):
    ts = 0
    board = game.getBoard()
    for t in allTerritories:
        (tP, tA) = board[t]
        if tP == p.n:
            ts = ts +1
    return ts

def cardPlus(plyr, n): 
     plyr.cards = plyr.cards + n  
                    
def cardCashin(plyr): #wxchanges 3 cards for more armies
    if plyr.cards >= 3:
        plyr.cards = plyr.cards - 3
        return True
    return False

def placeArmies(game, plyr, t, tArmy = 1):
    #assert either the territory is none or owned by plyr
    return boardUpdate(game, plyr, t, tArmy )

def moveArmy(game, p, t1, t2, numA ): # move armies from one territory to another
    board = game.getBoard()
    (plyr1, t1A) = board[t1]
    (plyr2, t2A) = board[t2] 
    #checks to see if a territory is a neighbor to another
    #need asserts for player owning both territories
    #needs assert for enough armies to move
    t1A= t1A - numA
    t2A = t2A + numA
    
    return boardUpdate(game, plyr1 , t1, t1Army, t2, t2Army, plyr2)



 #needs more rigourous testing   
def attack(game, attackT, defendT):
    board = game.getBoard()
    (aP, aArmy) = board[attackT]
    (dP, dArmy) = board[defendT] 

    # needs assert to check that a player is not attack themselves
    # needs assert that territors are neighbors
    # needs assert that territories are adjacent

    aNumTroops = min(aArmy - 1, 3)
    dNumTroops = min(dArmy, 2)
    plyr1 = aP

    (aLost, dLost) = battle(aNumTroops,dNumTroops)

    game.players[aP].totalArmies = game.players[aP].totalArmies - aLost
    game.players[dP].totalArmies = game.players[dP].totalArmies - dLost
    
    if(dLost == dArmy):
        plyr2 = aP
        t1Army = aArmy - aNumTroops
        t2Army = aNumTroops - aLost 
        game.players[dP].totalTerrs = game.players[dP].totalTerrs - 1
    else:
        plyr2 = dP
        t1Army = aArmy - aLost
        t2Army =  dArmy - dLost

    return boardUpdate(game, plr1, t1,t1Army, t2Army, plyr2) 

def battle(aNumTroops,dNumTroops):

    numTroops = min(aNumTroops, dNumTroops)
    aDiceValue = roll(aNumTroops) #attackDice
    dDiceValue = roll(dNumTroops) #deffendDice

    if aDiceValue[0] > dDiceValue[0]: #If attackDice value is greater than deffendDice value then:
        aLost = 0 #attackLost
        dLost = 1 #deffendLost
    else:
        aLost = 1 
        dLost = 0
    
    if numTroops == 2:
        if aDiceValue[1] > dDiceValue[1]:
            dLost = dLost + 1
        else:
            aLost = aLost + 1

    return  (aLost, dLost)

def roll(numDice):
    dice = [random.randint(1,6) for i in range(numDice)]
    dice.sort
    dice.reverse()
    return dice

#Testing

players = []
p1 = RandPlayer(0)
p2 = RandPlayer(1)
players = [p1,p2]

#Testing

game = Game(players) 
#board = game.getBoard()
#print plyrTers(game, p1)
#print p1.totalTerrs
#print p1.totalArmies
#print allotedArmies(game, p1)
#print plyrTersN(game, p1)
#print p1.cards

#intializes board
#board = {}
#board1 = {}
#totalPlyrs = 4

#for t in allTerritories:
#    plyrNum = random.randint(0,totalPlyrs*50)% totalPlyrs
#    board = placeArmies(board, plyrNum, t)
#    #print "###"+t + " "+str(board[t])

#for t in allTerritories:
#    plyrNum = 0
#    board1 = placeArmies(board1, plyrNum, t)

#tests for if a plyr controls a continent
#print str(doesPControlC(board, 0, asia))
#print str(doesPControlC(board1, 0, asia))